﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using Lion.WeiXin.Core.Models;
using System.Web.Script.Serialization;
namespace Lion.WeiXin
{
    /// <summary>
    /// 记录当前用户相关属性
    /// </summary>
    public class CurrentUser
    {
         static string _CURRENT_USER = "CurrentUser";
         static string _PERMISSION = "Permission";

        /// <summary> 
        /// 获取用户登录信息 
        /// </summary> 
        /// <returns></returns> 
        public static EmployeeModel GetUser()
        {
            if (HttpContext.Current.Request.IsAuthenticated)//是否通过身份验证 
            {
                HttpCookie authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];//获取cookie 
                FormsAuthenticationTicket Ticket = FormsAuthentication.Decrypt(authCookie.Value);//解密 
                JavaScriptSerializer json = new JavaScriptSerializer();             
                return json.Deserialize<EmployeeModel>(Ticket.UserData);//反序列化 
            }
            return null;
        } 

        public static void SignIn(EmployeeModel currentUser, List<CmsMenuModel> permissionMenus)
        {
            //记录用户属性
            HttpContext.Current.Session[_CURRENT_USER] = currentUser;
            //加载用户权限
            HttpContext.Current.Session[_PERMISSION] = permissionMenus;
        }


        public static void SignOut()
        {
            HttpContext.Current.Session[_CURRENT_USER] = null;
            HttpContext.Current.Session[_PERMISSION] = null;
        }

        public static int AID
        {
            get {
                if (HttpContext.Current.Session[_CURRENT_USER] != null)
                {
                    EmployeeModel model = (EmployeeModel)HttpContext.Current.Session[_CURRENT_USER];
                    return model.AID;
                }
                else
                {
                    return 0;
                }
            }
        }

        public static string UserName
        {
            get
            {
                if (HttpContext.Current.Session[_CURRENT_USER] != null)
                {
                    EmployeeModel model = (EmployeeModel)HttpContext.Current.Session[_CURRENT_USER];
                    return model.UserName;
                }
                else
                {
                    return string.Empty;
                }
            }
        }

        public static int RoleID
        {
            get
            {
                if (HttpContext.Current.Session[_CURRENT_USER] != null)
                {
                    EmployeeModel model = (EmployeeModel)HttpContext.Current.Session[_CURRENT_USER];
                    return model.Role_ID;
                }
                else
                {
                    return 0;
                }
            }
        }

        public static string SessionID
        {
            get
            {
                return HttpContext.Current.Session.SessionID;
            }
        }

        public static string IPAdress
        {
            get
            {
                return HttpContext.Current.Request.UserHostAddress;
            }
        }
        /// <summary>
        /// 当前用户拥有的权限菜单 
        /// </summary>
        public static List<CmsMenuModel> PermissionMenus
        {
            get {
                if (HttpContext.Current.Session[_PERMISSION] != null)
                {
                    return (List<CmsMenuModel>)HttpContext.Current.Session[_PERMISSION];
                }
                else
                {
                    return new List<CmsMenuModel>();
                }
            }
        }
        /// <summary>
        /// 微信专用
        /// </summary>
        public static string OpenId
        {
            get { return (string)HttpContext.Current.Session[KeysEnum.WeChatOpenId.ToString()]; }
            set { HttpContext.Current.Session[KeysEnum.WeChatOpenId.ToString()] = value; }
        }
        /// <summary>
        /// 微信用户
        /// </summary>
        public static WeChatUserModel WXUser
        {
            get { return (WeChatUserModel)HttpContext.Current.Session[KeysEnum.WeChatUser.ToString()]; }
            set { HttpContext.Current.Session[KeysEnum.WeChatUser.ToString()] = value; }
        }


    }
}